﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sl.Support.McuBytes {
	internal static class BinaryReader_Expand {
		static char[] delimiterChars = { '\0' };
		/// <summary>
		/// 从流中读取出长度固定的字符串，并以'\0'结尾。用于和MCU交互时读取信息数据结构中的字符串。
		/// </summary>
		/// <param name="br">二进制流</param>
		/// <param name="len">不论如何，流中指针的位置都会向前移动 len 长度，即使实际字符串结束标记提前出现。</param>
		/// <returns>读到的字符串</returns>
		internal static string ReadFixLenString(this BinaryReader br, int len, Encoding encoding = null) {
			if(encoding == null) encoding = Encoding.ASCII;
			return encoding.GetString(br.ReadBytes(len)).Split(delimiterChars)[0];
		}
	}
}
